//
//  SelectTimeCell.swift
//  koolcar
//
//  Created by xiantian on 2023/10/25.
//

import UIKit

class MaintenceTimeCell: UITableViewCell {
    var titleLab: UILabel!
    var collectionView: UICollectionView!
    var dataArray: [TimeAvailabilityDataModel] = []
    var selectIndex: NSInteger = 9999
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = Theme.viewBkgGrayColor
        
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize.init(width: (kScreenWidth - 28 - 32)/4, height: 50)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 180), collectionViewLayout: layout)
        self.collectionView.register(MaintenceTimeItemCell.self, forCellWithReuseIdentifier: "MaintenceTimeItemCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = Theme.viewBkgGrayColor
        self.collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        self.contentView.addSubview(self.collectionView)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        self.collectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.top.equalTo(0)
            make.height.equalTo(1)
            make.bottom.equalTo(0).priority(.high)
        }
    }
    
    func setMaintenceTimeCell(data: [TimeAvailabilityDataModel]) {
        self.dataArray = data
        self.collectionView.reloadData()
        
        if data.count > 0 {
            let rowNum = self.dataArray.count%4 == 0 ? self.dataArray.count/4 : self.dataArray.count/4 + 1
            let tempH = CGFloat(rowNum) * 50 + CGFloat(rowNum-1) * 10
            self.collectionView.snp.updateConstraints { make in
                make.top.equalTo(10)
                make.height.equalTo(tempH + 10)
            }
        }else{
            self.collectionView.snp.updateConstraints { make in
                make.top.equalTo(0)
                make.height.equalTo(1)
            }
        }
        self.updateConstraints()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension MaintenceTimeCell: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MaintenceTimeItemCell", for: indexPath) as! MaintenceTimeItemCell
        cell.tipBkgView.isHidden = true
        if self.dataArray.count > indexPath.row {
            let model = self.dataArray[indexPath.row]
            cell.setMaintenceTimeItemCell(data: model)
        }
        return cell
    }
    
    ///status 已满或不可选
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row {
            let model = self.dataArray[indexPath.row]
            if model.status == 2 || model.status == 0 { return }
            model.selectTimeBlock?()
        }

    }
}

